#Install Mediation package

install.packages("mediation")

#remove all objects from memory

rm(list=ls())	


#####################################################################################################
### TABLE 2 and TABLES 6.1-6.2 : economic treatment affects voting behavior via populist attitudes###
#####################################################################################################


library(foreign)

dataset_populism <- read.dta("/Users/SPardos/Dropbox/Project_Populism/BA project/Analysis/Trump_Replication files/Data/mediation_populism.dta")

library("mediation")
set.seed(2020)

#Predicting Trump vote

med.fit.vote.pop <- lm(populism ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.vote.pop <- lm(voteTrump ~ econ_control + populism + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.vote.pop <- mediate(med.fit.vote.pop, out.fit.vote.pop, treat = "econ_control", mediator = "populism", boot = TRUE, sims = 1000)

summary(med.fit.vote.pop)
summary(out.fit.vote.pop)
summary(med.out.vote.pop)


#Predicting Trump thermometer feeling

med.fit.trump.pop <- lm(populism ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.trump.pop <- lm(trump_feeling ~ econ_control + populism + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.trump.pop <- mediate(med.fit.trump.pop, out.fit.trump.pop, treat = "econ_control", mediator = "populism", boot = TRUE, sims = 1000)

summary(med.fit.trump.pop)
summary(out.fit.trump.pop)
summary(med.out.trump.pop)

#Predicting Biden thermometer feeling

med.fit.biden.pop <- lm(populism ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.biden.pop <- lm(biden_feeling ~ econ_control + populism + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.biden.pop <- mediate(med.fit.biden.pop, out.fit.biden.pop, treat = "econ_control", mediator = "populism", boot = TRUE, sims = 1000)

summary(med.fit.biden.pop)
summary(out.fit.biden.pop)
summary(med.out.biden.pop)


###################################################################################################################################
### TABLE 3, and TABLES 6.1 and 6.3 in the APPENDIX: economic treatment affects voting behavior via tax and spending preferences###
###################################################################################################################################


#Predicting Trump vote

med.fit.vote.spend <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.vote.spend <- lm(voteTrump ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.vote.spend <- mediate(med.fit.vote.spend, out.fit.vote.spend, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.vote.spend)
summary(out.fit.vote.spend)
summary(med.out.vote.spend)


#Predicting Trump thermometer feeling

med.fit.trump.spend <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.trump.spend <- lm(trump_feeling ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.trump.spend <- mediate(med.fit.trump.spend, out.fit.trump.spend, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.trump.spend)
summary(out.fit.trump.spend)
summary(med.out.trump.spend)

#Predicting Biden thermometer feeling

med.fit.biden.spend <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
out.fit.biden.spend <- lm(biden_feeling ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_populism)
med.out.biden.spend <- mediate(med.fit.biden.spend, out.fit.biden.spend, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.biden.spend)
summary(out.fit.biden.spend)
summary(med.out.biden.spend)


######################################################################################################################################
### TABLE 6.4 in the APPENDIX: economic treatment affects voting behavior via tax and spending preferences among richer respondents###
######################################################################################################################################

attach(dataset_populism)
dataset_rich <- dataset_populism[ which(income_binary=='1'),]
detach(dataset_populism)


#Predicting Trump vote

med.fit.vote.rich <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
out.fit.vote.rich <- lm(voteTrump ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
med.out.vote.rich <- mediate(med.fit.vote.rich, out.fit.vote.rich, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.vote.rich)
summary(out.fit.vote.rich)
summary(med.out.vote.rich)



#Predicting Trump thermometer feeling

med.fit.trump.rich <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
out.fit.trump.rich <- lm(trump_feeling ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
med.out.trump.rich <- mediate(med.fit.trump.rich, out.fit.trump.rich, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.trump.rich)
summary(out.fit.trump.rich)
summary(med.out.trump.rich)



#Predicting Biden thermometer feeling

med.fit.biden.rich <- lm(spending_unemp ~ econ_control + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
out.fit.biden.rich <- lm(biden_feeling ~ econ_control + spending_unemp + female + age + educ2 + educ3 + dom2 + dom3 + dom4 + ethnic3 + ethnic4 + relig2 + relig3, data= dataset_rich)
med.out.biden.rich <- mediate(med.fit.biden.rich, out.fit.biden.rich, treat = "econ_control", mediator = "spending_unemp", boot = TRUE, sims = 1000)

summary(med.fit.biden.rich)
summary(out.fit.biden.rich)
summary(med.out.biden.rich)
